from itsdangerous   import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall import settings

# 加密
def serect_openid(openid):

    # 1.创建实例对象
    s = Serializer(secret_key=settings.SECRET_KEY,expires_in=36000)

    # 2.组织数据
    data = {
        "openid":openid
    }
    # 加密
    new_data = s.dumps(data)

    # 返回加密数据
    return new_data.decode()


from itsdangerous import BadTimeSignature,BadData
def check_openid(serect_openid):
    # 1.创建实例对象
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=36000)

    # 2.获取解密数据'
    # 因为有可能会出现异常,所以来一个try操作
    try:
        data = s.loads(serect_openid)
    # except Exception:
    except BadData:
        return None
    else:
        return data.get("openid")




